package com.molyfun.parents.fragment;


import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.molyfun.parents.impl.IActivityBase;


public abstract class BaseFragment extends Fragment implements IActivityBase {

	@Override
	public void alert(String title, String msg, String positive,
					  DialogInterface.OnClickListener positiveListener, String negative,
					  DialogInterface.OnClickListener negativeListener) {
		if(getActivity() != null){
			((IActivityBase)getActivity()).alert(title, msg, positive, positiveListener, negative, negativeListener);
		}
	}

	@Override
	public void alert(String title, String msg, String positive,
					  DialogInterface.OnClickListener positiveListener, String negative,
					  DialogInterface.OnClickListener negativeListener,
					  Boolean isCanceledOnTouchOutside) {
		if(getActivity() != null){
			((IActivityBase)getActivity()).alert(title, msg, positive, positiveListener, negative, negativeListener,
					isCanceledOnTouchOutside);
		}
	}

	@Override
	public void toast(String msg, int period) {
		if(getActivity() != null){
			((IActivityBase)getActivity()).toast(msg, period);
		}
	}

	public void toast(String msg) {
		if(getActivity() != null){
			((IActivityBase)getActivity()).toast(msg, Toast.LENGTH_SHORT);
		}
	}

	@Override
	public void showProgressDialog(String msg) {
		if(getActivity() != null){
			((IActivityBase)getActivity()).showProgressDialog(msg);
		}
	}

	@Override
	public void showProgressDialog() {
		if(getActivity() != null){
			((IActivityBase)getActivity()).showProgressDialog(null);
		}
	}

	@Override
	public void showZhiboProgressDialog() {

	}

	public void showProgressDialog(final String msg, final boolean cancelable,
								   final DialogInterface.OnCancelListener cancelListener) {
		if(getActivity() != null){
			((IActivityBase)getActivity()).showProgressDialog(msg, cancelable, cancelListener);
		}
	}

	@Override
	public void dismissProgressDialog() {
		if(getActivity() != null){
			((IActivityBase)getActivity()).dismissProgressDialog();
		}
	}
	protected boolean isVisible;
	/**
	 * 在这里实现Fragment数据的缓加载.
	 * @param isVisibleToUser
	 */
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if(getUserVisibleHint()) {
			isVisible = true;
			onVisible();
		} else {
			isVisible = false;
			onInvisible();
		}
	}

	protected void onVisible(){
		lazyLoad();
	}

	protected void lazyLoad() {

	}

	protected void onInvisible(){}
}
